let category = {
  game: [
    { isSelect: true, title: 'Hadoop专区' },
    { isSelect: false, title: 'Dubbo主群' },],
  emotion: [
    { isSelect: true, title: 'java专区' },
    { isSelect: false, title: 'C语言专区' },]
}
let ajax = getApp().globalData.ajax;
let app = getApp();
let starUrl = app.globalData.fileUrl + 'd974f42cf782aac0d88da7313fe3af04_26_26.jpeg';
let blackStarUrl = app.globalData.fileUrl + '4f1e2758b6896f0ebb23768b7be5875c_26_26.jpeg'
let innerAudioContext = ''
Page({
  data: {
    navRouter: [],

    // bannerInfo: {
    //   imgSrc: 'http://image.tanwancat.com/176b0cdb86830013a73f61bfaca26346_500_249.jpeg',
    //     msg: '游戏陪练随时待命'
    // },

    anchorsList: [],
    categoryInfo: {},
    skip: 0,
    take: 5,
    hasNext: true,
    voiceIcon: 'http://image.tanwancat.com/aa6af4c843fc05169491106ca46c8053_20_20.jpeg',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //根据类型展示不同数据 目前只有游戏跟情感

    let categoryInfo = JSON.parse(options.categoryInfo);

    console.log(categoryInfo);

    this.setData({
      categoryInfo,
    })

    if (categoryInfo.category_id === 1) {
      wx.setNavigationBarTitle({
        title: "游戏陪练"
      })

      category.game.forEach(item => {
        if (item.title === this.data.categoryInfo.title) {
          item.isSelect = true;
        } else {
          item.isSelect = false;
        }
      })
      this.setData({
        navRouter: category.game,
      })


    } else {
      wx.setNavigationBarTitle({
        title: "情感陪伴"
      });
      category.emotion.forEach(item => {
        if (item.title === this.data.categoryInfo.title) {
          item.isSelect = true;
        } else {
          item.isSelect = false;
        }
      })
      this.setData({
        navRouter: category.emotion,
      })

    }

    switch (this.data.categoryInfo.skill_id) {
      case 1:
        this.setData({
          bannerInfo: {
            imgSrc: 'http://image.tanwancat.com/176b0cdb86830013a73f61bfaca26346_500_249.jpeg',
            msg: '游戏陪练随时待命'
          },
        });
        break;
      case 3:
        this.setData({
          bannerInfo: {
            imgSrc: 'http://image.tanwancat.com/57740c5e717701b0d87ad8de1bdb877a_399_200.jpeg',
            msg: '游戏陪练随时待命'
          },
        });
        break;
      case 6:
        this.setData({
          bannerInfo: {
            imgSrc: 'http://image.tanwancat.com/f38317843ad86ca7de0c1c63d2cb828a_718_286.jpeg',
            msg: '游戏陪练随时待命'
          },
        });
        break;
      case 7:
        this.setData({
          bannerInfo: {
            imgSrc: 'http://image.tanwancat.com/8d8bd489d2d519be099f2f2b1a6cc53a_718_286.jpeg',
            msg: '游戏陪练随时待命'
          },
        });
        break;
    }

    let json = {
      skill_id: this.data.categoryInfo.skill_id,
      skip: this.data.skip,
      take: this.data.take,
    }
    this.getData(json);
    this.getSkillList();

  },
  audioPlay (e) {
    let src = e.currentTarget.dataset.src
    if (innerAudioContext.src === src) {
      if (innerAudioContext.paused) {
        innerAudioContext.play()
      } else {
        innerAudioContext.pause()
      }
    } else {
      innerAudioContext.src = src
    }
  },
  //切换导航栏：
  chengName (e) {
    let index = e.target.dataset.index;
    let navRouter = this.data.navRouter;

    navRouter.forEach((item, ind) => {
      item.isSelect = ind === index ? true : false;
    })
    this.setData({
      navRouter,
      hasNext: true,
      skip: 0,
      anchorsList: [],
    });

    let skill_id = navRouter.find(item => item.isSelect).skill_id;
    switch (skill_id) {
      case 1:
        this.setData({
          bannerInfo: {
            imgSrc: 'http://image.tanwancat.com/176b0cdb86830013a73f61bfaca26346_500_249.jpeg',
            msg: '游戏陪练随时待命'
          },
        });
        break;
      case 3:
        this.setData({
          bannerInfo: {
            imgSrc: 'http://image.tanwancat.com/57740c5e717701b0d87ad8de1bdb877a_399_200.jpeg',
            msg: '游戏陪练随时待命'
          },
        });
        break;
      case 6:
        this.setData({
          bannerInfo: {
            imgSrc: 'http://image.tanwancat.com/f38317843ad86ca7de0c1c63d2cb828a_718_286.jpeg',
            msg: '游戏陪练随时待命'
          },
        });
        break;
      case 7:
        this.setData({
          bannerInfo: {
            imgSrc: 'http://image.tanwancat.com/8d8bd489d2d519be099f2f2b1a6cc53a_718_286.jpeg',
            msg: '游戏陪练随时待命'
          },
        });
        break;
    }
    let categoryInfo = Object.assign(this.data.categoryInfo, { skill_id })
    this.setData({
      categoryInfo
    })

    //切换导航栏重新请求数据源
    let json = {
      skill_id,
      skip: this.data.skip,
      take: this.data.take,
    }
    this.getData(json);

  },

  getData (json) {

    ajax('skill/person', json, ).then(res => {

      let anchorsList = res.data || [];

      if (!anchorsList.length) {
        this.setData({
          hasNext: false,
        })
      }

      anchorsList.forEach(item => {
        item.voiceIcon = this.data.voiceIcon;

        let heatNumber = Number(item.heat),
          heatList = [];
        for (let i = 1; i <= 5; i++) {
          if (i <= heatNumber) {
            heatList.push(starUrl);
          } else {
            heatList.push(blackStarUrl);
          }
        }
        item.heatList = heatList;

        if (item.user.gender === '男') {
          item.genderSrc = 'http://image.tanwancat.com/a3dd22f6723846c114a497f2f142acb6_20_20.jpeg'
        } else {
          item.genderSrc = 'http://image.tanwancat.com/38b571ac3c339057d78b59c364e2ef8b_20_20.jpeg'
        }

      })

      anchorsList = this.data.anchorsList.concat(anchorsList);
      this.setData({
        anchorsList,

      })
    })

    wx.stopPullDownRefresh();
  },

  getSkillList () {
    ajax('skill/list', { category_id: this.data.categoryInfo.category_id }, ).then(res => {
      res.data || [];
      res.data.forEach(item => {
        if (item.title === this.data.categoryInfo.title) {
          item.isSelect = true;
        } else {
          item.isSelect = false;
        }
      })
      this.setData({
        navRouter: res.data,
      })
    })
  },

  toDetail (e) {

    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: `../skillDetail/skillDetail?user_skill_id=${id}`,
      success: function () {
      },
    })
  },

  toGuide(){
	  wx.navigateTo({
		  url: '../guide/guide',
		  success: function () {
		  },
	  })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function (e) {
    // 使用 wx.createAudioContext 获取 audio 上下文 context
    innerAudioContext = wx.createInnerAudioContext()
    innerAudioContext.autoplay = true
    innerAudioContext.onPlay(() => {
      console.log('开始播放')
    })
    innerAudioContext.onPause(() => {
      console.log('暂停播放')
    })
    innerAudioContext.onError((res) => {
      console.log(res);
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    innerAudioContext.destroy()
  },
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    innerAudioContext.destroy()
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      skip: 0,
      anchorsList: [],
    })
    let json = {
      skill_id: this.data.categoryInfo.skill_id,
      skip: this.data.skip,
      take: this.data.take,
    }
    this.getData(json);

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (!this.data.hasNext) {
      wx.showToast({
        title: '没有更多主播',
        icon: 'loading',
        duration: 1000,
        mask: true
      })
      return
    }
    let skip = this.data.skip;
    skip += this.data.take;
    this.setData({
      skip,
    })
    let json = {
      skill_id: this.data.categoryInfo.skill_id,
      skip: this.data.skip,
      take: this.data.take,
    }
    this.getData(json)
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})